package com.xhh.user.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhh.model.common.dtos.ResponseResult;
import com.xhh.model.common.enums.AppHttpCodeEnum;
import com.xhh.model.user.pojos.Student;
import com.xhh.user.mapper.StudentMapper;
import com.xhh.user.service.StudentService;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    /**
     * 根据准考证号获取学生信息
     * @param studentCode
     * @return
     */
    @Override
    public ResponseResult getStuByCode(String studentCode) {
        // 1. 校验参数
        if (StrUtil.isEmpty(studentCode)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2. 构造查询条件
        Student student = lambdaQuery()
                .eq(Student::getStuNo, studentCode)
                .one();
        if (student == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 3. 返回结果
        return ResponseResult.okResult(student);
    }

    /**
     * 根据学生id获取学生信息
     * @param studentId
     * @return
     */
    @Override
    public ResponseResult getStuById(Long studentId) {
        // 1. 校验参数
        if (studentId == null || studentId == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2. 根据id查询学生数据
        Student student = getById(studentId);
        if (student == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 3. 返回结果
        return ResponseResult.okResult(student);
    }

    /**
     * 根据用户id获取学生信息
     * @param userId
     * @return
     */
    @Override
    public ResponseResult getStuByUId(Long userId) {
        // 1. 校验参数
        if (userId == null || userId == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2. 构建查询
        Student student = lambdaQuery()
                .eq(Student::getUserId, userId)
                .one();
        if (student == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 3. 返回结果
        return ResponseResult.okResult(student);
    }
}
